package com.chy.sprbootmanual.controller;

import com.chy.sprbootmanual.bean.Gallery;
import com.chy.sprbootmanual.bean.Order;
import com.chy.sprbootmanual.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

import static com.chy.sprbootmanual.jdbcutil.Constant.REDIRECT;
import static com.chy.sprbootmanual.jdbcutil.Constant.LOCALPATH;

import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@Slf4j
public class OrderContontroller {

    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/toEditable_Table",method = RequestMethod.GET)
    public String toEditable_Table(Model model){
        //获取用户列表
        List<Order> orderList = orderService.getAll();
        log.warn(String.valueOf(orderList));
        model.addAttribute("orderList",orderList);
        List<Gallery> imgList = orderService.getImgList();
        model.addAttribute("allImg",imgList);
        return  "table/editable_table";
    }

    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public  String delete(Integer id){
        //执行删除操作
        log.warn("========执行删除操作=======");
        System.out.println("id = " + id);
        orderService.delete(id);
        return REDIRECT;
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public  String save(Order order, HttpServletRequest request){
        //执行更新操作
        String addImgId = request.getParameter("select_picture");
        orderService.add(order,addImgId);
        return REDIRECT;
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public  String updateOrder(Order order, HttpServletRequest request){
        //执行修改操作
        String updateImgId = request.getParameter("select_picture");
        System.out.println("id = " + order.getId());
        System.out.println("updateImgId = " + updateImgId);
        System.out.println("order = " + order.getPicture());
        orderService.update(order,updateImgId);
        return REDIRECT;
    }

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile file) throws IOException {
        String name = file.getOriginalFilename();
        orderService.upload(name);
        file.transferTo(new File(LOCALPATH+file.getOriginalFilename()));
        return REDIRECT;
    }









    @RequestMapping("/toBasic_Table")
    public String toBasic_Table() {
        return "table/basic_table";
    }

    @RequestMapping("/toDynamic_Table")
    public String toDynamic_Table(){
       return  "table/dynamic_table";
    }

    @RequestMapping("/toResponsive_Table")
    public String toResponsive_Table(){
        return  "table/responsive_table";
    }

}
